pub mod data;
pub mod startup_system;
pub mod update_system;

use std::time::Duration;

use arg::Args;
use bevy::prelude::*;
use fastrand::Rng;

use crate::startup_system::StartUpPlugin;
use crate::update_system::UpdatePlugin;

fn main() {
    let args: Vec<String> = std::env::args().collect();
    println!("Args: {:?}", args);
    let args_ref = args.iter().skip(1).map(|s| s.as_str());
    let game_arg = GameArg::from_args(args_ref).unwrap();
    println!("GameArg: {:?}", game_arg);

    let mut app = App::new();
    // 添加参数和随机生成器
    app.insert_resource(GameRng(Rng::with_seed(game_arg.rand_seed)))
        .insert_resource(game_arg);
    // 设置固定时间系统的执行间隔，FPS 100
    app.insert_resource(Time::<Fixed>::from_duration(Duration::from_millis(10)));
    // 添加组件
    app.add_plugins(DefaultPlugins);
    app.add_plugins(StartUpPlugin).add_plugins(UpdatePlugin);
    app.run();
}

#[derive(Args, Resource, Debug, Clone)]
pub struct GameArg {
    #[arg(short = "c", long = "count", default_value = "10")]
    pub process_exec_count: usize,
    #[arg(
        short = "s",
        long = "seed",
        default_value = "fastrand::u64(u64::MIN..=u64::MAX)"
    )]
    pub rand_seed: u64,
    #[arg(
        short = "l",
        long = "link",
        default_value = "\"./demo_answer.exe\".to_string()"
    )]
    pub link_program_path: String,
    #[arg(short = "w", long = "waiting", default_value = "2.5")]
    pub end_waiting_time: f32,
}

#[derive(Resource, Debug, Deref, DerefMut)]
pub struct GameRng(Rng);
